package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.KeywordMapper;
import org.linlinjava.litemall.db.models.Keyword;
import org.linlinjava.litemall.db.service.KeywordService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, Keyword>
    implements KeywordService {

    @Override
    public Keyword queryDefault() {
        return baseMapper.selectOne(new LambdaQueryWrapper<Keyword>().eq(Keyword::getIsDefault, true));
    }

    @Override
    public List<Keyword> queryHots() {
        return baseMapper.selectList(new LambdaQueryWrapper<Keyword>().eq(Keyword::getIsHot, true));
    }

    @Override
    public IPage<Keyword> queryByKeyword(String keyword, Integer pageNo, Integer pageSize) {
        Page<Keyword> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Keyword> query = new QueryWrapper<>();
        query.like("keyword", keyword);
        //去重
        query.select("DISTINCT keyword");
       return baseMapper.selectPage(page, query);
    }

    @Override
    public IPage<Keyword> querySelective(String keyword, String url, Integer pageNo, Integer pageSize) {
        Page<Keyword> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Keyword> query = new QueryWrapper<>();
        query.like(!StringUtils.isEmpty(keyword),"keyword", keyword);
        query.like(!StringUtils.isEmpty(url),"url", url);
        return baseMapper.selectPage(page, query);
    }
}
